﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebDAVSharp.Server.Stores;
using WebDAVSharp.Server.Stores.DiskStore;

namespace AzureStorageLight
{
    public class AppWebDavStoreCollection : AppWebDavStoreItem, IWebDavStoreCollection
    {
        public AppWebDavStoreCollection(AppWebDavStoreCollection parentCollection, string path)
            : base(parentCollection, path)
        {

        }

        public IEnumerable<IWebDavStoreItem> Items
        {
            get
            {
                if (_items == null) EnsuteItems();
                return _items.Select(x => x.Type != "File" ? new AppWebDavStoreCollection(this, ItemPath + x.Name +"/") : (IWebDavStoreItem)new AppWebDavStoreDocument(this, ItemPath + x.Name));
            }
        }

        public void EnsuteItems(bool reload=false)
        {
            if (_items == null || reload)
            {
                _items = Service.GetFiles(this.ItemPath, reload);
            }
        }

        private List<StorageItem> LoadCachedData()
        {
            List<StorageItem> data;
            var listdata = Service.GetFiles(ItemPath);
            data = listdata.OrderBy(x => x.Type).ThenBy(x => x.Name).ToList();
            return data;
        }

        private List<StorageItem> _items;

        public IWebDavStoreItem CopyItemHere(IWebDavStoreItem source, string destinationName, bool includeContent)
        {
            var target = Service.CopyAsync(source.ItemPath, ItemPath + destinationName).Result;
            return new AppWebDavStoreDocument(this, ItemPath + destinationName);
        }

        public IWebDavStoreCollection CreateCollection(string name)
        {
            var path = ItemPath + name + "/";
            var info = Service.EnsureItem(path).Result;
            return new AppWebDavStoreCollection(this, path);
        }

        public IWebDavStoreDocument CreateDocument(string name)
        {
            var path = ItemPath + name;
            var info = Service.EnsureItem(path).Result;
            return new AppWebDavStoreDocument(this, path);
        }

        public void Delete(IWebDavStoreItem item)
        {
            var target = Service.DeleteAsync(item.ItemPath).Result;
        }

        public IWebDavStoreItem GetItemByName(string name)
        {
            if (_items == null) EnsuteItems();
            var item = _items.Where(x => x.Name == name).FirstOrDefault();
            if (item != null)
            {
                if (item.Type != "File")
                {
                    return new AppWebDavStoreCollection(this, ItemPath + item.Name + "/");
                }
                else
                {
                    return new AppWebDavStoreDocument(this, ItemPath + item.Name);
                }
            }
            return null;
        }

        public IWebDavStoreItem MoveItemHere(IWebDavStoreItem source, string destinationName)
        {
            if (source is AppWebDavStoreDocument)
            {
                var mvItems = Service.MoveAsync(source.ItemPath, ItemPath + destinationName).Result;
                return new AppWebDavStoreDocument(this, ItemPath + destinationName);
            }
            else if (source is AppWebDavStoreCollection)
            {
                var mvItems = Service.MoveAsync(source.ItemPath, ItemPath + destinationName + "/").Result;
                return new AppWebDavStoreCollection(this, ItemPath + destinationName + "/");
            }
            else
            {
                throw new NotSupportedException();
            }
        }

    }
}
